home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / javnl005.zip / javnl005.txt < prev   
Text File  |  1996-05-14  |  16KB  |  510 lines

  1. Issue #005
  2. May, 1996
  3.  
  4.  
  5. Contents:
  6.  
  7. Comparing C/C++ with Java Part 5 - Multiple Inheritance
  8. Sorting in Java
  9. Java Program Packaging Part 3 - Protected/Default
  10. Performance - Method Call Overhead
  11.  
  12.  
  13. COMPARING C/C++ WITH JAVA PART 5 - MULTIPLE INHERITANCE
  14.  
  15. Suppose that you have two C++ classes:
  16.  
  17.         class A {
  18.         public:
  19.                 int x;
  20.                 void f();
  21.         };
  22.  
  23.         class B {
  24.         public:
  25.                 int y;
  26.                 void g();
  27.         };
  28.  
  29. and a third class that derives or inherits ("extends") from both of
  30. these:
  31.  
  32.         class C : public A, public B {
  33.                 // stuff
  34.         };
  35.  
  36. Inheriting from multiple base classes is called "multiple inheritance"
  37. or MI for short.
  38.  
  39. Like templates that we discussed in issue #004, multiple inheritance
  40. adds to the complexity of C++, while offering a solution to some
  41. particular kinds of programming problems.  One type of issue with
  42. programming using MI is in deciding which of a set of conflicting
  43. inherited names "wins".
  44.  
  45. Java does not have multiple inheritance.  It does, however, have a way
  46. of doing some of what MI is intended for, relative to inheriting
  47. attributes.  In Java there is a language feature called an interface,
  48. which looks like this:
  49.  
  50.         public interface Orderable {
  51.                 // return < 0 if p1 < p2, 0 if equal, > 0 if p1 > p2
  52.                 public int compareTo(Object p1, Object p2);
  53.         }
  54.  
  55. This looks somewhat like a class, except that all the methods are
  56. abstract, that is, are not implemented immediately but serve as
  57. placeholders.
  58.  
  59. What is an interface used for?  Suppose that you want to ensure that a
  60. particular class has certain properties, in the present case that it
  61. defines the method compareTo() to order objects.  You can enforce this
  62. by requiring that the class implement interface Orderable:
  63.  
  64.         public class xxx implements Orderable {
  65.                 public int compareTo(Object p1, Object p2)
  66.                 {
  67.                         int n1 = ((Integer)p1).intValue();
  68.                         int n2 = ((Integer)p2).intValue();
  69.                         return n1 - n2;
  70.                 }
  71.         }
  72.  
  73. Why do we care about this?  In the next section we will talk about
  74. sorting in Java.  To write a semi-general sort method, it's necessary
  75. to assume that the objects within a Vector of objects to be sorted
  76. have some ordering method available to rank them.  Java by default has
  77. no way of ordering two arbitrary objects.
  78.  
  79. By defining an Orderable interface, and either sorting a vector of
  80. Orderables (that is, a vector of object types where the type is
  81. defined to implement Orderable), or sorting a Vector of Objects with a
  82. compareTo() method passed to the sort routine via a method wrapper
  83. (see next section), we can ensure that a compareTo() method is
  84. available.
  85.  
  86. An interface is sort of like a base class for a class.  If you have
  87. an Orderable reference:
  88.  
  89.         Orderable or = null;
  90.  
  91. it's possible to assign to it a reference to an object type that
  92. implements Orderable:
  93.  
  94.         xxx x = new xxx();
  95.  
  96.         or = x;
  97.  
  98. Again, if we have an Orderable object reference, we can be sure that a
  99. method call like:
  100.  
  101.         or.compareTo(p1, p2)
  102.  
  103. will be valid.
  104.  
  105.  
  106. SORTING IN JAVA
  107.  
  108. In the last issue we talked about sorting in Java and said that
  109. there's no approach to sorting that's really similar to qsort() in
  110. C/C++.  A couple of people wrote to me about this and suggested a
  111. technique that does in fact have some similarities.
  112.  
  113. The idea is to write a sort method that accepts a Vector of Object
  114. references, along with a class object instance that is a wrapper for a
  115. compareTo() method as illustrated above.  The sort routine will
  116. iterate over the vector and call out to the compare method to
  117. determine the ordering of any two objects.  Specifically, this would
  118. look like:
  119.  
  120.         import java.util.Vector;
  121.         
  122.         // Orderable interface
  123.         
  124.         interface Orderable {
  125.                 // return < 0 if p1 < p2, 0 if equal, > 0 if p1 > p2
  126.                 public int compareTo(Object p1, Object p2);
  127.         };
  128.         
  129.         // wrapper for a compareTo() method for ordering Strings
  130.         
  131.         class Ord implements Orderable {
  132.                 public int compareTo(Object p1, Object p2)
  133.                 {
  134.                         return ((String)p1).compareTo(((String)p2));
  135.                 }
  136.         }
  137.         
  138.         public class Sort {
  139.         
  140.                 public static void sort(Vector v, Orderable or)
  141.                 {
  142.                         if (v == null || or == null)
  143.                                 ; // give error of some kind
  144.         
  145.                         // get vector size
  146.         
  147.                         int n = v.size();
  148.         
  149.                         // sort
  150.         
  151.                         for (int i = 0; i < n - 1; i++) {
  152.                                 for (int j = i + 1; j < n; j++) {
  153.         
  154.                                         // do comparison
  155.         
  156.                                         if (or.compareTo(v.elementAt(i),
  157.                                             v.elementAt(j)) > 0) {
  158.                                                 Object ti = v.elementAt(i);
  159.                                                 Object tj = v.elementAt(j);
  160.                                                 v.setElementAt(tj, i);
  161.                                                 v.setElementAt(ti, j);
  162.                                         }
  163.                                 }
  164.                         }
  165.                 }
  166.         
  167.                 // driver program
  168.         
  169.                 public static void main(String args[])
  170.                 {
  171.                         Vector v = new Vector();
  172.                         int N = 100;
  173.                         int i = 0;
  174.         
  175.                         // add some strings
  176.         
  177.                         for (i = 0; i < N; i++)
  178.                                 v.addElement("xxx" + i);
  179.         
  180.                         // sort them
  181.         
  182.                         Sort.sort(v, new Ord());
  183.         
  184.                         // display the sorted list
  185.         
  186.                         for (i = 0; i < N; i++)
  187.                                 System.out.println((String)v.elementAt(i));
  188.                 }
  189.         
  190.         }
  191.  
  192. This code can be tuned in various ways (starting with the sort
  193. algorithm) but is illustrative of the technique.  We can implement any
  194. sort strategy we want on a Vector of objects simply by changing the
  195. ordering method.  For example, saying:
  196.  
  197.         class Ordrev implements Orderable {
  198.                 public int compareTo(Object p1, Object p2)
  199.                 {
  200.                         return -((String)p1).compareTo(((String)p2));
  201.                 }
  202.         }
  203.  
  204.         ...
  205.  
  206.         Sort.sort(v, new Ordrev());
  207.  
  208. will reverse the sorting order for Strings.  In this particular
  209. example, Strings have a compareTo() method already defined, and we
  210. simply cast the Object references to Strings and call this method.
  211. Note that if the wrong compareTo() wrapper instance is used, then an
  212. illegal cast will be attempted, resulting in an exception being
  213. thrown.  For example, the above case expects a String, and will
  214. generate an exception ("ClassCastException") if the objects we pass in
  215. are actually Integers.  The "instanceof" operator can be used to help
  216. sort things out.
  217.  
  218. In production code we'd have Orderable defined in a separate source
  219. file.  Ord might or might not be in its own file, depending on
  220. stylistic preferences.  Ord is simply a wrapper for a particular
  221. compareTo() method.  In C or C++ we would pass in a function pointer
  222. directly, but Java has no user-visible pointers and no global
  223. functions.
  224.  
  225. If we critique this approach and compare it to qsort(), there are some
  226. notable differences and some similarities:
  227.  
  228.         1.  This approach is higher-level than qsort(), because it
  229.         doesn't fool with pointers and sizes of objects and so on.
  230.  
  231.         2.  This approach cannot be used to directly sort vectors of
  232.         fundamental data types like int or double.  They must be
  233.         sorted using object wrappers.
  234.  
  235.         3.  Both approaches require calling out to a function or
  236.         method that is used to order el